<%
#########################################################################
# Copyright Thoughtworks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#########################################################################
-%>

    java="$(<%= ENV['JRE_BINARY_LOCATOR'] %>)"
    if [ -n "${java}" ]; then
      echo "(info) Configuring <%= name %> to default to package-provided Java runtime located at '${java}', unless overridden by user."
      sed -ie "s:^\\(wrapper.java.command=\\).*:\\1${java}:" /usr/share/<%= name %>/wrapper-config/wrapper.conf
    else
      echo "(warning) <%= name %> cannot locate package-installed Java runtime (may need to install recommended dependencies?). Will look for default or pre-configured Java runtime."
    fi

    configured_java_command=$(
      grep -oP '^wrapper.java.command\s*=\s*\K.*' /usr/share/<%= name %>/wrapper-config/wrapper-properties.conf || \
      grep -oP '^wrapper.java.command\s*=\s*\K.*' /usr/share/<%= name %>/wrapper-config/wrapper.conf
    )

    if [ -x "$(command -v ${configured_java_command})" ]; then
      echo "(info) <%= name %> will use the configured Java runtime located at '${configured_java_command}'"
    else
      >&2 echo "(error) Current <%= name %> configuration is looking for Java runtime at '${configured_java_command}' but it cannot be found."
      >&2 echo "(error) <%= name %> cannot locate a Java runtime to use. Please ensure a compatible Java runtime is available by doing one of:"
      >&2 echo "        - Installing the 'recommended' package dependencies which include a compatible runtime OR"
      >&2 echo "        - Pre-configuring location of a compatible 'java' executable using 'wrapper.java.command=/path/to/bin/java' in /usr/share/<%= name %>/wrapper-config/wrapper-properties.conf OR"
      >&2 echo "        - Ensuring a compatible Java runtime is available by default on the PATH"
      exit 1
    fi